package com.ruoyi.system.service.impl;
import com.google.common.collect.Lists;

import cn.hutool.core.date.DateUtil;
import java.util.*;
import java.util.stream.Collectors;
import javax.validation.Validator;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.SysUserEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.dto.UserUpdateVo;
import com.ruoyi.system.domain.vo.UserEducationVo;
import com.ruoyi.system.domain.vo.UserUnDoVo;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.*;
import liquibase.pro.packaged.H;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.util.ObjectUtils;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysUserServiceImpl implements ISysUserService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    protected Validator validator;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private UserInfoChangeMapper userInfoChangeMapper;

    @Autowired
    private SysPostMapper sysPostMapper;

    @Autowired
    private IMeetingSummaryService meetingSummaryService;
    @Autowired
    private IMeetingCollectService meetingCollectService;
    @Autowired
    private IActivitySummaryService activitySummaryService;
    @Autowired
    private IActivityCollectService activityCollectService;
    @Autowired
    private ISysUserDevelopService sysUserDevelopService;

    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SysUser> selectUserList(SysUser user)
    {
        List<SysUser> sysUserList = userMapper.selectUserList(user);
        for(SysUser sysUser : sysUserList){
            String postName = selectUserPostGroup(sysUser.getUserName());
            sysUser.setPostName(postName);
            Long flowPartyMemberDept = sysUser.getFlowPartyMemberDept();
            SysDept sysDept = sysDeptMapper.selectDeptById(flowPartyMemberDept);
            if(!ObjectUtils.isEmpty(sysDept)){
                sysUser.setFlowPartyMemberDeptName(sysDept.getDeptName());
            }else{
                sysUser.setFlowPartyMemberDeptName("");
            }
        }
        return sysUserList;
    }

    /**
     * 根据条件分页查询已分配用户角色列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SysUser> selectAllocatedList(SysUser user)
    {
        return userMapper.selectAllocatedList(user);
    }

    /**
     * 根据条件分页查询未分配用户角色列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SysUser> selectUnallocatedList(SysUser user)
    {
        return userMapper.selectUnallocatedList(user);
    }

    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserByUserName(String userName)
    {
        return userMapper.selectUserByUserName(userName);
    }

    /**
     * 通过用户ID查询用户
     *
     * @param userId 用户ID
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserById(Long userId)
    {
        SysUser user = userMapper.selectUserById(userId);
        String postName = selectUserPostGroup(user.getUserName());
        user.setPostName(postName);
        List<Long> pointIdList = selectUserPostIdGroup(user.getUserName());
        Long[] points = pointIdList.stream().toArray(Long[]::new);
        user.setPostIds(points);
        return user;
    }

    /**
     * 查询用户所属角色组
     *
     * @param userName 用户名
     * @return 结果
     */
    @Override
    public String selectUserRoleGroup(String userName)
    {
        List<SysRole> list = roleMapper.selectRolesByUserName(userName);
        if (CollectionUtils.isEmpty(list))
        {
            return StringUtils.EMPTY;
        }
        return list.stream().map(SysRole::getRoleName).collect(Collectors.joining(","));
    }

    /**
     * 查询用户所属岗位组
     *
     * @param userName 用户名
     * @return 结果
     */
    @Override
    public String selectUserPostGroup(String userName)
    {
        List<SysPost> list = postMapper.selectPostsByUserName(userName);
        if (CollectionUtils.isEmpty(list))
        {
            return StringUtils.EMPTY;
        }
        return list.stream().map(SysPost::getPostName).collect(Collectors.joining(","));
    }

    /**
     * 查询用户所属岗位组id
     *
     * @param userName 用户名
     * @return 结果
     */
    public List<Long> selectUserPostIdGroup(String userName)
    {
        List<Long> resultList = new ArrayList<>();
        List<SysPost> list = postMapper.selectPostsByUserName(userName);
        if (CollectionUtils.isEmpty(list))
        {
            return resultList;
        }
        resultList = list.stream().map(SysPost::getPostId).collect(Collectors.toList());
        return resultList;
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public boolean checkUserNameUnique(SysUser user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUser info = userMapper.checkUserNameUnique(user.getUserName());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkPhoneUnique(SysUser user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUser info = userMapper.checkPhoneUnique(user.getPhonenumber());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验email是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkEmailUnique(SysUser user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUser info = userMapper.checkEmailUnique(user.getEmail());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验用户是否允许操作
     *
     * @param user 用户信息
     */
    @Override
    public void checkUserAllowed(SysUser user)
    {
        if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin())
        {
            throw new ServiceException("不允许操作超级管理员用户");
        }
    }

    /**
     * 校验用户是否有数据权限
     *
     * @param userId 用户id
     */
    @Override
    public void checkUserDataScope(Long userId)
    {
        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
        {
            SysUser user = new SysUser();
            user.setUserId(userId);
            List<SysUser> users = SpringUtils.getAopProxy(this).selectUserList(user);
            if (StringUtils.isEmpty(users))
            {
                throw new ServiceException("没有权限访问用户数据！");
            }
        }
    }

    /**
     * 新增保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUser(SysUser user)
    {
        if(StringUtils.isBlank(user.getPassword())){
            String phonenumber = user.getPhonenumber();
            if(!StringUtils.isBlank(phonenumber)){
                String passwordString = StringUtils.substring(phonenumber, 5);
                user.setPassword(passwordString);
            }
        }
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        // 新增用户信息
        int rows = userMapper.insertUser(user);
        // 新增用户岗位关联
        insertUserPost(user);
        // 新增用户与角色管理
        insertUserRole(user);
        return rows;
    }

    /**
     * 注册用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public boolean registerUser(SysUser user)
    {
        return userMapper.insertUser(user) > 0;
    }

    /**
     * 修改保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUser(SysUser user)
    {
        Long userId = user.getUserId();
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(user);
        // 删除用户与岗位关联
        userPostMapper.deleteUserPostByUserId(userId);
        // 新增用户与岗位管理
        insertUserPost(user);
        //添加变更信息
        SysUser sysUser = userMapper.selectUserById(userId);
        insertUserInfoChange(sysUser,user);
        return userMapper.updateUser(user);
    }

    public void insertUserInfoChange(SysUser changeBeforeUser,SysUser changeAfterUser){

        String userNameAfter = changeAfterUser.getUserName();
        if(StringUtils.isNotBlank(userNameAfter)){
            String userNameBefore = changeBeforeUser.getUserName();
            if(StringUtils.isBlank(userNameBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.USER_NAME.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(userNameAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!userNameBefore.equals(userNameAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.USER_NAME.getFieldValue());
                    userInfoChange.setChangeBefore(userNameBefore);
                    userInfoChange.setChangeAfter(userNameAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String sexAfter = changeAfterUser.getSex();
        if(StringUtils.isNotBlank(sexAfter)){
            String sexBefore = changeBeforeUser.getSex();
            if(StringUtils.isBlank(sexBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.SEX.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(sexAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!sexBefore.equals(sexAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.SEX.getFieldValue());
                    userInfoChange.setChangeBefore(sexBefore);
                    userInfoChange.setChangeAfter(sexAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String nationAfter = changeAfterUser.getNation();
        if(StringUtils.isNotBlank(nationAfter)){
            String nationBefore = changeBeforeUser.getNation();
            if(StringUtils.isBlank(nationBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.NATION.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(nationAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!nationBefore.equals(nationAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.NATION.getFieldValue());
                    userInfoChange.setChangeBefore(nationBefore);
                    userInfoChange.setChangeAfter(nationAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String nativePlaceAfter = changeAfterUser.getNativePlace();
        if(StringUtils.isNotBlank(nativePlaceAfter)){
            String nativePlaceBefore = changeBeforeUser.getNativePlace();
            if(StringUtils.isBlank(nativePlaceBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.NATIVE_PLACE.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(nativePlaceBefore);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!nativePlaceBefore.equals(nativePlaceAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.NATIVE_PLACE.getFieldValue());
                    userInfoChange.setChangeBefore(nativePlaceBefore);
                    userInfoChange.setChangeAfter(nativePlaceBefore);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String HomeAdressAfter = changeAfterUser.getHomeAdress();
        if(StringUtils.isNotBlank(HomeAdressAfter)){
            String homeAdressBefore = changeBeforeUser.getHomeAdress();
            if(StringUtils.isBlank(homeAdressBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.HOME_ADRESS.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(HomeAdressAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!homeAdressBefore.equals(HomeAdressAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.HOME_ADRESS.getFieldValue());
                    userInfoChange.setChangeBefore(homeAdressBefore);
                    userInfoChange.setChangeAfter(HomeAdressAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String idCardAfter = changeAfterUser.getIdCard();
        if(StringUtils.isNotBlank(idCardAfter)){
            String idCardBefore = changeBeforeUser.getIdCard();
            if(StringUtils.isBlank(idCardBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.ID_CARD.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(idCardAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!idCardBefore.equals(idCardAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.ID_CARD.getFieldValue());
                    userInfoChange.setChangeBefore(idCardBefore);
                    userInfoChange.setChangeAfter(idCardAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String birthdayAfter = changeAfterUser.getBirthday();
        if(StringUtils.isNotBlank(birthdayAfter)){
            String birthdayBefore = changeBeforeUser.getBirthday();
            if(StringUtils.isBlank(birthdayBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.BIRTHDAY.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(birthdayAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!birthdayBefore.equals(birthdayAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.BIRTHDAY.getFieldValue());
                    userInfoChange.setChangeBefore(birthdayBefore);
                    userInfoChange.setChangeAfter(birthdayAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String phonenumberAfter = changeAfterUser.getPhonenumber();
        if(StringUtils.isNotBlank(birthdayAfter)){
            String phonenumberBefore = changeBeforeUser.getPhonenumber();
            if(StringUtils.isBlank(phonenumberBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.PHONENUMBER.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(phonenumberAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!phonenumberBefore.equals(phonenumberAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.PHONENUMBER.getFieldValue());
                    userInfoChange.setChangeBefore(phonenumberBefore);
                    userInfoChange.setChangeAfter(phonenumberAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String highestEducationAfter = changeAfterUser.getHighestEducation();
        if(StringUtils.isNotBlank(highestEducationAfter)){
            String highestEducationBefore = changeBeforeUser.getHighestEducation();
            if(StringUtils.isBlank(highestEducationBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.HITHEST_EDUCATION.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(highestEducationAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!highestEducationBefore.equals(highestEducationAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.HITHEST_EDUCATION.getFieldValue());
                    userInfoChange.setChangeBefore(highestEducationBefore);
                    userInfoChange.setChangeAfter(highestEducationAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String graduateSchoolAfter = changeAfterUser.getGraduateSchool();
        if(StringUtils.isNotBlank(graduateSchoolAfter)){
            String graduateSchoolBefore = changeBeforeUser.getGraduateSchool();
            if(StringUtils.isBlank(graduateSchoolBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.GRADUATE_SCHOOL.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(graduateSchoolAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!graduateSchoolBefore.equals(graduateSchoolAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.GRADUATE_SCHOOL.getFieldValue());
                    userInfoChange.setChangeBefore(graduateSchoolBefore);
                    userInfoChange.setChangeAfter(graduateSchoolAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String majorAfter = changeAfterUser.getMajor();
        if(StringUtils.isNotBlank(majorAfter)){
            String majorBefore = changeBeforeUser.getMajor();
            if(StringUtils.isBlank(majorBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.MAJOR.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(majorAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!majorBefore.equals(majorAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.MAJOR.getFieldValue());
                    userInfoChange.setChangeBefore(majorBefore);
                    userInfoChange.setChangeAfter(majorAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String majorNameAfter = changeAfterUser.getMajorName();
        if(StringUtils.isNotBlank(majorNameAfter)){
            String majorNameBefore = changeBeforeUser.getMajorName();
            if(StringUtils.isBlank(majorNameBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.MAJOR_NAME.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(majorNameAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else {
                if(!majorNameBefore.equals(majorNameAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.MAJOR_NAME.getFieldValue());
                    userInfoChange.setChangeBefore(majorNameBefore);
                    userInfoChange.setChangeAfter(majorNameAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        Long deptIdAfter = changeAfterUser.getDeptId();
        if(!ObjectUtils.isEmpty(deptIdAfter)){
            Long deptIdBefore = changeBeforeUser.getDeptId();
            if(ObjectUtils.isEmpty(deptIdBefore)){
                SysDept sysDeptAfter = sysDeptMapper.selectDeptById(deptIdAfter);
                String deptNameBefore = "";
                String deptNameAfter = "";
                if(!ObjectUtils.isEmpty(sysDeptAfter)){
                    deptNameAfter = sysDeptAfter.getDeptName();
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.DEPT_NAME.getFieldValue());
                    userInfoChange.setChangeBefore(deptNameBefore);
                    userInfoChange.setChangeAfter(deptNameAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }

            }else{
                SysDept sysDeptBefore = sysDeptMapper.selectDeptById(deptIdBefore);
                SysDept sysDeptAfter = sysDeptMapper.selectDeptById(deptIdAfter);
                String deptNameBefore = "";
                String deptNameAfter = "";
                if(!ObjectUtils.isEmpty(sysDeptBefore)){
                    deptNameBefore = sysDeptBefore.getDeptName();
                }
                if(!ObjectUtils.isEmpty(sysDeptAfter)){
                    deptNameAfter = sysDeptAfter.getDeptName();
                }
                if(!deptIdBefore.equals(deptIdAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.DEPT_NAME.getFieldValue());
                    userInfoChange.setChangeBefore(deptNameBefore);
                    userInfoChange.setChangeAfter(deptNameAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        Long[] postIdsAfter = changeAfterUser.getPostIds();
        if(!ObjectUtils.isEmpty(postIdsAfter)){
            List<String> postNameAfterList = new ArrayList();
            String postNameAfter = "";
            String postNameBefore = "";
            for(Long postId : postIdsAfter){
                SysPost sysPost = sysPostMapper.selectPostById(postId);
                String postName = sysPost.getPostName();
                postNameAfterList.add(postName);
            }
            postNameAfter = StringUtils.join(postNameAfterList, ",");
            Long[] postIdsBefore = changeBeforeUser.getPostIds();
            if(!ObjectUtils.isEmpty(postIdsBefore)){
                List<String> postNameBeforeList = new ArrayList();
                List<Long> postIdsBeforeList = Arrays.asList(postIdsBefore).stream().sorted().collect(Collectors.toList());
                List<Long> postIdsAfterList = Arrays.asList(postIdsAfter).stream().sorted().collect(Collectors.toList());
                String lstStr1 = StringUtils.join(postIdsBeforeList, ",");
                String lstStr2 = StringUtils.join(postIdsAfterList, ",");
                if(!lstStr1.equals(lstStr2)){
                    for(Long postId : postIdsBefore){
                        SysPost sysPost = sysPostMapper.selectPostById(postId);
                        String postName = sysPost.getPostName();
                        postNameBeforeList.add(postName);
                    }
                    postNameBefore = StringUtils.join(postNameBeforeList, ",");
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.POST_NAME.getFieldValue());
                    userInfoChange.setChangeBefore(postNameBefore);
                    userInfoChange.setChangeAfter(postNameAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }else{
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.POST_NAME.getFieldValue());
                userInfoChange.setChangeBefore(postNameBefore);
                userInfoChange.setChangeAfter(postNameAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }

        }


        String administrativePostAfter = changeAfterUser.getAdministrativePost();
        if(StringUtils.isNotBlank(administrativePostAfter)){
            String administrativePostBefore = changeBeforeUser.getAdministrativePost();
            if(StringUtils.isBlank(administrativePostBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.ADMINSTRATIVE_POST.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(administrativePostAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!administrativePostBefore.equals(administrativePostAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.ADMINSTRATIVE_POST.getFieldValue());
                    userInfoChange.setChangeBefore(administrativePostBefore);
                    userInfoChange.setChangeAfter(administrativePostAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String workTimeAfter = changeAfterUser.getWorkTime();
        if(StringUtils.isNotBlank(workTimeAfter)){
            String workTimeBefore = changeBeforeUser.getWorkTime();
            if(StringUtils.isBlank(workTimeBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.WORK_TIME.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(administrativePostAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!workTimeBefore.equals(workTimeAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.WORK_TIME.getFieldValue());
                    userInfoChange.setChangeBefore(workTimeBefore);
                    userInfoChange.setChangeAfter(administrativePostAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String enterUnitTimeAfter = changeAfterUser.getEnterUnitTime();
        if(StringUtils.isNotBlank(enterUnitTimeAfter)){
            String enterUnitTimeBefore = changeBeforeUser.getEnterUnitTime();
            if(StringUtils.isBlank(enterUnitTimeBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.ENTER_UNIT_TIME.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(administrativePostAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!enterUnitTimeBefore.equals(enterUnitTimeAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.ENTER_UNIT_TIME.getFieldValue());
                    userInfoChange.setChangeBefore(enterUnitTimeBefore);
                    userInfoChange.setChangeAfter(administrativePostAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String employmentModeAfter = changeAfterUser.getEmploymentMode();
        if(StringUtils.isNotBlank(employmentModeAfter)){
            String getEmploymentModeBefore = changeBeforeUser.getEmploymentMode();
            if(StringUtils.isBlank(getEmploymentModeBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.EMPLOUMENT_MODE.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(employmentModeAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);

            }else{
                if(!getEmploymentModeBefore.equals(employmentModeAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.EMPLOUMENT_MODE.getFieldValue());
                    userInfoChange.setChangeBefore(getEmploymentModeBefore);
                    userInfoChange.setChangeAfter(employmentModeAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String readyPartyTimeAfter = changeAfterUser.getReadyPartyTime();
        if(StringUtils.isNotBlank(readyPartyTimeAfter)){
            String readyPartyTimeBefore = changeBeforeUser.getReadyPartyTime();
            if(StringUtils.isBlank(readyPartyTimeBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.READY_PARTY_TIME.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(readyPartyTimeAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!readyPartyTimeBefore.equals(readyPartyTimeAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.READY_PARTY_TIME.getFieldValue());
                    userInfoChange.setChangeBefore(readyPartyTimeBefore);
                    userInfoChange.setChangeAfter(readyPartyTimeAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String partyTimeAfter = changeAfterUser.getPartyTime();
        if(StringUtils.isNotBlank(readyPartyTimeAfter)){
            String partyTimeBefore = changeBeforeUser.getPartyTime();
            if(StringUtils.isBlank(partyTimeBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.PARTY_TIME.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(partyTimeAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!partyTimeBefore.equals(partyTimeAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.PARTY_TIME.getFieldValue());
                    userInfoChange.setChangeBefore(partyTimeBefore);
                    userInfoChange.setChangeAfter(partyTimeAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String organizationRelationTimeAfter = changeAfterUser.getOrganizationRelationTime();
        if(StringUtils.isNotBlank(readyPartyTimeAfter)){
            String organizationRelationTimeBefore = changeBeforeUser.getOrganizationRelationTime();
            if(StringUtils.isBlank(organizationRelationTimeBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.REALTION_TIME.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(organizationRelationTimeAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!organizationRelationTimeBefore.equals(organizationRelationTimeAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.REALTION_TIME.getFieldValue());
                    userInfoChange.setChangeBefore(organizationRelationTimeBefore);
                    userInfoChange.setChangeAfter(organizationRelationTimeAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String statusAfter = changeAfterUser.getStatus();
        if(StringUtils.isNotBlank(statusAfter)){
            String statusBefore = changeBeforeUser.getStatus();
            if(StringUtils.isBlank(statusBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.STATUS.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(statusAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!statusBefore.equals(statusAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.STATUS.getFieldValue());
                    userInfoChange.setChangeBefore(statusBefore);
                    userInfoChange.setChangeAfter(statusAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String partyTypeAfter = changeAfterUser.getPartyType();
        if(StringUtils.isNotBlank(partyTypeAfter)){
            String partyTypeBefore = changeBeforeUser.getPartyType();
            if(StringUtils.isBlank(partyTypeBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.PARTY_TYPE.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(partyTypeAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!partyTypeBefore.equals(partyTypeAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.PARTY_TYPE.getFieldValue());
                    userInfoChange.setChangeBefore(partyTypeBefore);
                    userInfoChange.setChangeAfter(partyTypeAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        Long flowPartyMemberDeptAfter = changeAfterUser.getFlowPartyMemberDept();
        if(!ObjectUtils.isEmpty(flowPartyMemberDeptAfter)){
            Long flowPartyMemberDeptBefore = changeBeforeUser.getFlowPartyMemberDept();
            if(ObjectUtils.isEmpty(flowPartyMemberDeptBefore)){
                SysDept sysDeptAfter = sysDeptMapper.selectDeptById(flowPartyMemberDeptAfter);
                String deptNameBefore = "";
                String deptNameAfter = "";
                if(!ObjectUtils.isEmpty(sysDeptAfter)){
                    deptNameAfter = sysDeptAfter.getDeptName();
                }
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.MEMBER_DEPT.getFieldValue());
                userInfoChange.setChangeBefore(deptNameBefore);
                userInfoChange.setChangeAfter(deptNameAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);

            }else {
                SysDept sysDeptBefore = sysDeptMapper.selectDeptById(flowPartyMemberDeptBefore);
                SysDept sysDeptAfter = sysDeptMapper.selectDeptById(flowPartyMemberDeptAfter);
                String deptNameBefore = "";
                String deptNameAfter = "";
                if(!ObjectUtils.isEmpty(sysDeptBefore)){
                    deptNameBefore = sysDeptBefore.getDeptName();
                }
                if(!ObjectUtils.isEmpty(sysDeptAfter)){
                    deptNameAfter = sysDeptAfter.getDeptName();
                }
                if(!flowPartyMemberDeptBefore.equals(flowPartyMemberDeptAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.MEMBER_DEPT.getFieldValue());
                    userInfoChange.setChangeBefore(deptNameBefore);
                    userInfoChange.setChangeAfter(deptNameAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String recordLocationAfter = changeAfterUser.getRecordLocation();
        if(StringUtils.isNotBlank(partyTypeAfter)){
            String recordLocationBefore = changeBeforeUser.getRecordLocation();
            if(StringUtils.isBlank(recordLocationBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.RECORD_LOCATION.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(recordLocationAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!recordLocationBefore.equals(recordLocationAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.RECORD_LOCATION.getFieldValue());
                    userInfoChange.setChangeBefore(recordLocationBefore);
                    userInfoChange.setChangeAfter(recordLocationAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

        String remarkAfter = changeAfterUser.getRemark();
        if(StringUtils.isNotBlank(remarkAfter)){
            String remarkBefore = changeBeforeUser.getRemark();
            if(StringUtils.isBlank(remarkBefore)){
                UserInfoChange userInfoChange = new UserInfoChange();
                userInfoChange.setChangeItem(SysUserEnum.REMARK.getFieldValue());
                userInfoChange.setChangeBefore("");
                userInfoChange.setChangeAfter(remarkAfter);
                userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                userInfoChange.setCreateTime(DateUtils.getNowDate());
                userInfoChange.setUserId(changeBeforeUser.getUserId());
                userInfoChangeMapper.insertUserInfoChange(userInfoChange);
            }else{
                if(!remarkBefore.equals(recordLocationAfter)){
                    UserInfoChange userInfoChange = new UserInfoChange();
                    userInfoChange.setChangeItem(SysUserEnum.REMARK.getFieldValue());
                    userInfoChange.setChangeBefore(remarkBefore);
                    userInfoChange.setChangeAfter(remarkAfter);
                    userInfoChange.setCreateBy(changeAfterUser.getUpdateBy());
                    userInfoChange.setCreateTime(DateUtils.getNowDate());
                    userInfoChange.setUserId(changeBeforeUser.getUserId());
                    userInfoChangeMapper.insertUserInfoChange(userInfoChange);
                }
            }
        }

    }

    /**
     * 用户授权角色
     *
     * @param userId 用户ID
     * @param roleIds 角色组
     */
    @Override
    @Transactional
    public void insertUserAuth(Long userId, Long[] roleIds)
    {
        userRoleMapper.deleteUserRoleByUserId(userId);
        insertUserRole(userId, roleIds);
    }

    /**
     * 修改用户状态
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUserStatus(SysUser user)
    {
        return userMapper.updateUser(user);
    }

    /**
     * 修改用户基本信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUserProfile(SysUser user)
    {
        String password = user.getPassword();
        if(StringUtils.isNotBlank(password)){
            user.setPassword(SecurityUtils.encryptPassword(password));
        }
        return userMapper.updateUser(user);
    }

    /**
     * 修改用户头像
     *
     * @param user 用户名
     * @return 结果
     */
    @Override
    public AjaxResult updateUserAvatar(SysUser user)
    {
        try {
            userMapper.updateUserAvatar(user);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("修改失败");
        }
    }

    /**
     * 重置用户密码
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int resetPwd(SysUser user)
    {
        return userMapper.updateUser(user);
    }

    /**
     * 重置用户密码
     *
     * @param userName 用户名
     * @param password 密码
     * @return 结果
     */
    @Override
    public int resetUserPwd(String userName, String password)
    {
        return userMapper.resetUserPwd(userName, password);
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(SysUser user)
    {
        this.insertUserRole(user.getUserId(), user.getRoleIds());
    }

    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    public void insertUserPost(SysUser user)
    {
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotEmpty(posts))
        {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>(posts.length);
            for (Long postId : posts)
            {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            userPostMapper.batchUserPost(list);
        }
    }

    /**
     * 新增用户角色信息
     *
     * @param userId 用户ID
     * @param roleIds 角色组
     */
    public void insertUserRole(Long userId, Long[] roleIds)
    {
        if (StringUtils.isNotEmpty(roleIds))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
            for (Long roleId : roleIds)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }
            userRoleMapper.batchUserRole(list);
        }
    }

    /**
     * 通过用户ID删除用户
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserById(Long userId)
    {
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 删除用户与岗位表
        userPostMapper.deleteUserPostByUserId(userId);
        return userMapper.deleteUserById(userId);
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserByIds(Long[] userIds)
    {
        for (Long userId : userIds)
        {
            checkUserAllowed(new SysUser(userId));
            checkUserDataScope(userId);
        }
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(userIds);
        // 删除用户与岗位关联
        userPostMapper.deleteUserPost(userIds);
        return userMapper.deleteUserByIds(userIds);
    }

    /**
     * 导入用户数据
     *
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = configService.selectConfigByKey("sys.user.initPassword");
        String passwordString = "";
        for (SysUser user : userList)
        {
            String phonenumber = user.getPhonenumber();
            user.setUserName(phonenumber);
            if(!StringUtils.isBlank(phonenumber)){
                passwordString = StringUtils.substring(phonenumber, 5);
                user.setPassword(passwordString);
            }else{
                failureMsg.insert(0, "很抱歉，导入失败！联系方式不能为空！");
            }
            try
            {
                // 验证是否存在这个用户
                SysUser u = userMapper.selectUserByUserName(user.getUserName());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setPassword(SecurityUtils.encryptPassword(password));
                    user.setCreateBy(operName);
                    //关联部门
                    String deptName = user.getDeptName();
                    String flowPartyMemberDeptName = user.getFlowPartyMemberDeptName();
                    if(StringUtils.isNotBlank(deptName)){
                        SysDept sysDept = sysDeptMapper.selectDeptByDeptName(deptName);
                        if(ObjectUtils.isEmpty(sysDept)){
                            failureMsg.insert(0, "很抱歉，导入失败！部门名称不正确！");
                        }else{
                            user.setDeptId(sysDept.getDeptId());
                        }
                    }
                    if(StringUtils.isNotBlank(flowPartyMemberDeptName)){
                        SysDept sysDept = sysDeptMapper.selectDeptByDeptName(flowPartyMemberDeptName);
                        if(ObjectUtils.isEmpty(sysDept)){
                            failureMsg.insert(0, "很抱歉，导入失败！流动党员所在党支部名称不正确！");
                        }else{
                            user.setDeptId(sysDept.getDeptId());
                        }
                    }
                    userMapper.insertUser(user);
                    //关联党内职务
                    String postName = user.getPostName();
                    if(StringUtils.isNotBlank(postName)){
                        List<String> postNameList = Arrays.asList(postName.split(","));
                        List<SysUserPost> userPostList = new ArrayList<>();
                        for(String name : postNameList){
                            SysPost sysPost = sysPostMapper.selectPostByName(name);
                            if(ObjectUtils.isEmpty(sysPost)){
                                failureMsg.insert(0, "很抱歉，导入失败！党内职务名称不正确！");
                            }else{
                                //添加人员职位关系
                                SysUserPost sysUserPost = new SysUserPost();
                                sysUserPost.setUserId(user.getUserId());
                                sysUserPost.setPostId(sysPost.getPostId());
                                userPostList.add(sysUserPost);
                            }
                        }
                        userPostMapper.batchUserPost(userPostList);
                    }
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
                    checkUserAllowed(u);
                    checkUserDataScope(u.getUserId());
                    user.setUserId(u.getUserId());
                    user.setUpdateBy(operName);
                    userMapper.updateUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    /**
     * 通过手机号查询用户
     *
     * @param phonenumber 手机号
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserByPhonenumber(String phonenumber){
        return userMapper.selectUserByPhonenumber(phonenumber);
    }


    /**
     * 查询是否有权限编辑数据
     */
    @Override
    public Boolean userPower(UserUpdateVo userUpdateVo){
        String userDeptId = userUpdateVo.getUserDeptId();
        //查询人员对应部门
        SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(userDeptId));
        Long parentId = sysDept.getParentId();
        //总部有权限编辑全部数据
        if(0L==parentId){
            return true;
        }else{
            //分部人员只能编辑本部门数据
            String dataDeptId = userUpdateVo.getDataDeptId();
            if(userDeptId.equals(dataDeptId)){
                return true;
            }else{
                return false;
            }

        }
    }

    /**
     * 查询是否有权限编辑数据
     */
    @Override
    public List<UserUnDoVo> selectMyTask(UserUnDoVo userUnDoVo){
        List<UserUnDoVo> resultList = new ArrayList<>();
        String taskType = userUnDoVo.getTaskType();
        //事项类型 0:会议上传 1：会后收集 2：活动上传 3：活动收集 4：发展党员谈话
        if(StringUtils.isNotBlank(taskType)){
            if(taskType.equals("0")){
                //待办的会议上传文件
                List<UserUnDoVo> userUnDoVos1 = meetingSummaryService.selectMeetingUploadTask(userUnDoVo);
                String status = userUnDoVo.getStatus();
                //状态 0:进行中 1：超期
                if(StringUtils.isNotBlank(status)){
                    if(status.equals("0")){
                        userUnDoVos1 = userUnDoVos1.stream().filter(o -> o.getStatus().equals("0")).collect(Collectors.toList());
                    }else if(status.equals("1")){
                        userUnDoVos1 = userUnDoVos1.stream().filter(o -> o.getStatus().equals("1")).collect(Collectors.toList());
                    }
                }
                //1:今日到期 2：近7天提醒
                String queryTime = userUnDoVo.getQueryTime();
                if("1".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos1.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        boolean sameDay = DateUtil.isSameDay(nowDate, endTime);
                        if(!sameDay){
                            iterator.remove();
                        }
                    }
                }else if("2".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos1.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        Date dateTime = DateUtil.offsetDay(endTime, -7);
                        if(nowDate.compareTo(dateTime)<0){
                            iterator.remove();
                        }
                    }
                }
                resultList = userUnDoVos1.stream().sorted(Comparator.comparing(UserUnDoVo::getEndTime)).collect(Collectors.toList());
            }else if(taskType.equals("1")){
                List<UserUnDoVo> userUnDoVos2 = meetingCollectService.selectMeetingCollectTask(userUnDoVo);
                String status = userUnDoVo.getStatus();
                //状态 0:进行中 1：超期
                if(StringUtils.isNotBlank(status)){
                    if(status.equals("0")){
                        userUnDoVos2 = userUnDoVos2.stream().filter(o -> o.getStatus().equals("0")).collect(Collectors.toList());
                    }else if(status.equals("1")){
                        userUnDoVos2 = userUnDoVos2.stream().filter(o -> o.getStatus().equals("1")).collect(Collectors.toList());
                    }
                }
                //1:今日到期 2：近7天提醒
                String queryTime = userUnDoVo.getQueryTime();
                if("1".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos2.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        boolean sameDay = DateUtil.isSameDay(nowDate, endTime);
                        if(!sameDay){
                            iterator.remove();
                        }
                    }
                }else if("2".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos2.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        Date dateTime = DateUtil.offsetDay(endTime, -7);
                        if(nowDate.compareTo(dateTime)<0){
                            iterator.remove();
                        }
                    }
                }
                resultList = userUnDoVos2.stream().sorted(Comparator.comparing(UserUnDoVo::getEndTime)).collect(Collectors.toList());
            }else if(taskType.equals("2")){
                List<UserUnDoVo> userUnDoVos3 = activitySummaryService.selectActivityUploadTask(userUnDoVo);
                String status = userUnDoVo.getStatus();
                //状态 0:进行中 1：超期
                if(StringUtils.isNotBlank(status)){
                    if(status.equals("0")){
                        userUnDoVos3 = userUnDoVos3.stream().filter(o -> o.getStatus().equals("0")).collect(Collectors.toList());
                    }else if(status.equals("1")){
                        userUnDoVos3 = userUnDoVos3.stream().filter(o -> o.getStatus().equals("1")).collect(Collectors.toList());
                    }
                }
                //1:今日到期 2：近7天提醒
                String queryTime = userUnDoVo.getQueryTime();
                if("1".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos3.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        boolean sameDay = DateUtil.isSameDay(nowDate, endTime);
                        if(!sameDay){
                            iterator.remove();
                        }
                    }
                }else if("2".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos3.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        Date dateTime = DateUtil.offsetDay(endTime, -7);
                        if(nowDate.compareTo(dateTime)<0){
                            iterator.remove();
                        }
                    }
                }
                resultList = userUnDoVos3.stream().sorted(Comparator.comparing(UserUnDoVo::getEndTime)).collect(Collectors.toList());
            }else if(taskType.equals("3")){
                List<UserUnDoVo> userUnDoVos4 = activityCollectService.selectActivityCollectTask(userUnDoVo);
                String status = userUnDoVo.getStatus();
                //状态 0:进行中 1：超期
                if(StringUtils.isNotBlank(status)){
                    if(status.equals("0")){
                        userUnDoVos4 = userUnDoVos4.stream().filter(o -> o.getStatus().equals("0")).collect(Collectors.toList());
                    }else if(status.equals("1")){
                        userUnDoVos4 = userUnDoVos4.stream().filter(o -> o.getStatus().equals("1")).collect(Collectors.toList());
                    }
                }
                //1:今日到期 2：近7天提醒
                String queryTime = userUnDoVo.getQueryTime();
                if("1".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos4.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        boolean sameDay = DateUtil.isSameDay(nowDate, endTime);
                        if(!sameDay){
                            iterator.remove();
                        }
                    }
                }else if("2".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos4.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        Date dateTime = DateUtil.offsetDay(endTime, -7);
                        if(nowDate.compareTo(dateTime)<0){
                            iterator.remove();
                        }
                    }
                }
                resultList = userUnDoVos4.stream().sorted(Comparator.comparing(UserUnDoVo::getEndTime)).collect(Collectors.toList());
            }else if(taskType.equals("4")){
                //发展党员谈话
                List<UserUnDoVo> userUnDoVos5 = new ArrayList<>();
                List<SysUserStage1> sysUserStage1List = sysUserDevelopService.selectTalkTask(userUnDoVo);
                for(SysUserStage1 stage: sysUserStage1List ){
                    UserUnDoVo userUnDoVo1 = new UserUnDoVo();
                    userUnDoVo1.setTaskType("4");
                    userUnDoVo1.setTaskName(stage.getDevelopUserName());
                    Date step1FinishTime = stage.getStep1FinishTime();
                    Date dateTime = DateUtil.offsetMonth(step1FinishTime, 1);
                    userUnDoVo1.setEndTime(dateTime);
                    Date nowDate = DateUtils.getNowDate();
                    if(dateTime.compareTo(nowDate)<0){
                        //过期
                        userUnDoVo1.setStatus("1");
                    }else{
                        userUnDoVo1.setStatus("0");
                    }
                    userUnDoVos5.add(userUnDoVo1);
                }
                Date endTime1 = userUnDoVo.getEndTime();
                if(!ObjectUtils.isEmpty(endTime1)){
                    userUnDoVos5 = userUnDoVos5.stream().filter(o -> o.getEndTime().before(endTime1)).collect(Collectors.toList());
                }
                String status = userUnDoVo.getStatus();
                //状态 0:进行中 1：超期
                if(StringUtils.isNotBlank(status)){
                    if(status.equals("0")){
                        userUnDoVos5 = userUnDoVos5.stream().filter(o -> o.getStatus().equals("0")).collect(Collectors.toList());
                    }else if(status.equals("1")){
                        userUnDoVos5 = userUnDoVos5.stream().filter(o -> o.getStatus().equals("1")).collect(Collectors.toList());
                    }
                }
                //1:今日到期 2：近7天提醒
                String queryTime = userUnDoVo.getQueryTime();
                if("1".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos5.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        boolean sameDay = DateUtil.isSameDay(nowDate, endTime);
                        if(!sameDay){
                            iterator.remove();
                        }
                    }
                }else if("2".equals(queryTime)){
                    Iterator<UserUnDoVo> iterator = userUnDoVos5.iterator();
                    while(iterator.hasNext()){
                        Date nowDate = DateUtils.getNowDate();
                        Date endTime = iterator.next().getEndTime();
                        Date dateTime = DateUtil.offsetDay(endTime, -7);
                        if(nowDate.compareTo(dateTime)<0){
                            iterator.remove();
                        }
                    }
                }
                resultList = userUnDoVos5.stream().sorted(Comparator.comparing(UserUnDoVo::getEndTime)).collect(Collectors.toList());
            }
        }else{
            //待办的会议上传文件
            List<UserUnDoVo> userUnDoVos1 = meetingSummaryService.selectMeetingUploadTask(userUnDoVo);
            //待办的会后收集
            List<UserUnDoVo> userUnDoVos2 = meetingCollectService.selectMeetingCollectTask(userUnDoVo);
            //活动文件上传
            List<UserUnDoVo> userUnDoVos3 = activitySummaryService.selectActivityUploadTask(userUnDoVo);
            //活动收集
            List<UserUnDoVo> userUnDoVos4 = activityCollectService.selectActivityCollectTask(userUnDoVo);
            //发展党员谈话
            List<UserUnDoVo> userUnDoVos5 = new ArrayList<>();
            List<SysUserStage1> sysUserStage1List = sysUserDevelopService.selectTalkTask(userUnDoVo);
            for(SysUserStage1 stage: sysUserStage1List ){
                UserUnDoVo userUnDoVo1 = new UserUnDoVo();
                userUnDoVo1.setTaskType("4");
                userUnDoVo1.setTaskName(stage.getDevelopUserName());
                Date step1FinishTime = stage.getStep1FinishTime();
                Date dateTime = DateUtil.offsetMonth(step1FinishTime, 1);
                userUnDoVo1.setEndTime(dateTime);
                Date nowDate = DateUtils.getNowDate();
                if(dateTime.compareTo(nowDate)<0){
                    //过期
                    userUnDoVo1.setStatus("1");
                }else{
                    userUnDoVo1.setStatus("0");
                }
                userUnDoVos5.add(userUnDoVo1);
            }
            resultList.addAll(userUnDoVos1);
            resultList.addAll(userUnDoVos2);
            resultList.addAll(userUnDoVos3);
            resultList.addAll(userUnDoVos4);
            resultList.addAll(userUnDoVos5);
            String status = userUnDoVo.getStatus();
            //状态 0:进行中 1：超期
            if(StringUtils.isNotBlank(status)){
                if(status.equals("0")){
                    resultList = resultList.stream().filter(o -> o.getStatus().equals("0")).collect(Collectors.toList());
                }else if(status.equals("1")){
                    resultList = resultList.stream().filter(o -> o.getStatus().equals("1")).collect(Collectors.toList());
                }
            }
            //1:今日到期 2：近7天提醒
            String queryTime = userUnDoVo.getQueryTime();
            if("1".equals(queryTime)){
                Iterator<UserUnDoVo> iterator = resultList.iterator();
                while(iterator.hasNext()){
                    Date nowDate = DateUtils.getNowDate();
                    Date endTime = iterator.next().getEndTime();
                    boolean sameDay = DateUtil.isSameDay(nowDate, endTime);
                    if(!sameDay){
                        iterator.remove();
                    }
                }
            }else if("2".equals(queryTime)){
                Iterator<UserUnDoVo> iterator = resultList.iterator();
                while(iterator.hasNext()){
                    Date nowDate = DateUtils.getNowDate();
                    Date endTime =  iterator.next().getEndTime();
                    Date dateTime = DateUtil.offsetDay(endTime, -7);
                    if(nowDate.compareTo(dateTime)<0){
                        iterator.remove();
                    }
                }
            }
            resultList = resultList.stream().sorted(Comparator.comparing(UserUnDoVo::getEndTime)).collect(Collectors.toList());
        }
        return resultList;
    }


    /**
     * 年龄结构
     */
    @Override
    public Map<String,List<String>> selectAgeStructure(){
        HashMap<String,List<String>> map = new HashMap();
        SysUser user = new SysUser();
        List<SysUser> sysUserList = userMapper.selectUserList(user);
        Integer count1 = 0;
        Integer count2 = 0;
        Integer count3 = 0;
        Integer count4 = 0;
        for(SysUser sysUser : sysUserList){
            String birthday = sysUser.getBirthday();
            if(StringUtils.isNotBlank(birthday)){
                Date date = DateUtil.parse(birthday, "yyyy-MM-dd");
                int ageByBirth = getAgeByBirth(date);
                if(ageByBirth>=18&&ageByBirth<30){
                    count1++;
                }else if(ageByBirth>=30&&ageByBirth<40){
                    count2++;
                }else if(ageByBirth>=40&&ageByBirth<50){
                    count3++;
                }else if(ageByBirth>=50){
                    count4++;
                }
            }
        }
        List<String> ageList = new ArrayList();
        ageList.add("18-30岁");
        ageList.add("30-40岁");
        ageList.add("40-50岁");
        ageList.add("50岁以上");

        List<String> ageCountList = new ArrayList();
        ageCountList.add(String.valueOf(count1));
        ageCountList.add(String.valueOf(count2));
        ageCountList.add(String.valueOf(count3));
        ageCountList.add(String.valueOf(count4));

        map.put("y",ageList);
        map.put("x",ageCountList);
        return map;
    }

    /**
     *
     * 根据出生年月日计算年龄
     */
    public static int getAgeByBirth(Date birthday){

        //获取当前时间
        Calendar cal = Calendar.getInstance();

        //获取出生日期的Calendar对象
        Calendar bir = Calendar.getInstance();
        bir.setTime(birthday);
        //如果出生日期大于当前日期，则返回0
        if(cal.before(birthday)){
            return 0;
        }
        //取出当前年月日
        int nowYear = cal.get(Calendar.YEAR);
        int nowMonth = cal.get(Calendar.MONTH);
        int nowDay = cal.get(Calendar.DAY_OF_MONTH);

        //取出出生日期的年月日
        int birthYear = bir.get(Calendar.YEAR);
        int birthMonth = bir.get(Calendar.MONTH);
        int birthDay = bir.get(Calendar.DAY_OF_MONTH);

        //计算年份
        int age = nowYear - birthYear;

        //计算月份和日，看看是否大于当前月日，如果小于则减去一岁
        if(nowMonth < birthMonth || (nowMonth == birthMonth && nowDay < birthDay)){
            age--;
        }
        return age;
    }


    /**
     * 学历结构
     */
    @Override
    public List<UserEducationVo> selectEducationStructure(){
        List<UserEducationVo> resultList = new ArrayList();
        SysUser user = new SysUser();
        List<SysUser> sysUserList = userMapper.selectUserList(user);
        Integer count1 = 0;
        Integer count2 = 0;
        Integer count3 = 0;
        Integer count4 = 0;
        Integer count5 = 0;
        for(SysUser sysUser : sysUserList){
            String highestEducation = sysUser.getHighestEducation();
            if(StringUtils.isNotBlank(highestEducation)){
                if("高中".equals(highestEducation)){
                    count1++;
                }
                else if("大专".equals(highestEducation)){
                    count2++;
                }
                else if("本科".equals(highestEducation)){
                    count3++;
                }
                else if("硕士".equals(highestEducation)){
                    count4++;
                }else{
                    count5++;
                }
            }
        }
        UserEducationVo userEducationVo1 = new UserEducationVo();
        userEducationVo1.setName("高中");
        userEducationVo1.setValue(count1);
        UserEducationVo userEducationVo2 = new UserEducationVo();
        userEducationVo2.setName("大专");
        userEducationVo2.setValue(count2);
        UserEducationVo userEducationVo3 = new UserEducationVo();
        userEducationVo3.setName("本科");
        userEducationVo3.setValue(count3);
        UserEducationVo userEducationVo4 = new UserEducationVo();
        userEducationVo4.setName("硕士");
        userEducationVo4.setValue(count4);
        UserEducationVo userEducationVo5 = new UserEducationVo();
        userEducationVo5.setName("其他");
        userEducationVo5.setValue(count5);
        resultList.add(userEducationVo1);
        resultList.add(userEducationVo2);
        resultList.add(userEducationVo3);
        resultList.add(userEducationVo4);
        resultList.add(userEducationVo5);
        return resultList;
    }

    /**
     * 民族分布
     */
    @Override
    public Map<String,List<String>> selectNationalHabitat(){

        SysUser user = new SysUser();
        List<SysUser> sysUserList = userMapper.selectUserList(user);
        Map<String, List<SysUser>> collect = sysUserList.stream().filter(o->o.getNation()!=null).collect(Collectors.groupingBy(SysUser::getNation));
        List<String> nationCountList = new ArrayList<>();
        List<String> nationNameList = new ArrayList<>();
        for(Map.Entry<String, List<SysUser>> entry : collect.entrySet()){
            String key = entry.getKey();
            List<SysUser> value = entry.getValue();
            nationCountList.add(String.valueOf(value.size()));
            nationNameList.add(key);
        }
        HashMap<String,List<String>> map = new HashMap();
        map.put("y",nationCountList);
        map.put("x",nationNameList);
        return map;
    }


    /**
     * 籍贯分布
     */
    @Override
    public List<UserEducationVo>  selectNativeStructure(){
        List<UserEducationVo> userEducationVolist=Lists.newArrayList();
        SysUser user = new SysUser();
        List<SysUser> sysUserList = userMapper.selectUserList(user);
        Map<String, List<SysUser>> collect = sysUserList.stream().filter(o->o.getNativePlace()!=null).collect(Collectors.groupingBy(o->StringUtils.substringBefore(o.getNativePlace(),",")));

        for(Map.Entry<String, List<SysUser>> entry : collect.entrySet()){
            String key = entry.getKey();
            List<SysUser> value = entry.getValue();
            UserEducationVo userEducationVo = new UserEducationVo();
            userEducationVo.setName(key);
            userEducationVo.setValue(value.size());
            userEducationVolist.add(userEducationVo);
        }
        return userEducationVolist;
    }

    /**
     * 男女比例
     */
    @Override
    public Map<String,Long>  selectSexStructure(){
        SysUser user = new SysUser();
        List<SysUser> sysUserList = userMapper.selectUserList(user);
        long manCount = sysUserList.stream().filter(o -> o.getSex().equals("0")).count();
        long womanCount = sysUserList.stream().filter(o -> o.getSex().equals("1")).count();
        //排除admin
        int size = sysUserList.size()-1;
        long man = manCount * 100 / size ;
        long woman = womanCount * 100 / size ;
        Map<String,Long> resultMap = new HashMap<>();
        resultMap.put("man",man);
        resultMap.put("woman",woman);
        return resultMap;
    }

    /**
     * 组织数据
     */
    @Override
    public Map<String,Integer>  selectDpetUserData(){
        Map<String, Integer> resultMap = new HashMap<>();
        SysUser sysUser = new SysUser();
        //党员总数(排除admin)
        Integer count = userMapper.selectUserByPartyTypeAndStatus(sysUser);
        resultMap.put("total",count-1);
        //正式党员
        sysUser.setPartyType("0");
        Integer count0 = userMapper.selectUserByPartyTypeAndStatus(sysUser);
        resultMap.put("partyType0",count0);
        //预备党员
        sysUser.setPartyType("1");
        Integer count1 = userMapper.selectUserByPartyTypeAndStatus(sysUser);
        resultMap.put("partyType0",count1);
        //流动党员
        sysUser.setPartyType("2");
        Integer count2 = userMapper.selectUserByPartyTypeAndStatus(sysUser);
        resultMap.put("partyType2",count2);
        //在岗党员（排除admin）
        sysUser.setPartyType("");
        sysUser.setStatus("0");
        Integer onJobCount = userMapper.selectUserByPartyTypeAndStatus(sysUser);
        resultMap.put("onJobCount",onJobCount-1);
        //发展党员
        SysUserDevelop sysUserDevelop = new SysUserDevelop();
        List<SysUserDevelop> sysUserDevelops = sysUserDevelopService.selectSysUserDevelopList(sysUserDevelop);
        resultMap.put("developCount",sysUserDevelops.size());
        //党支部数量
        SysDept sysDept = new SysDept();
        List<SysDept> sysDepts = sysDeptMapper.selectDeptList(sysDept);
        resultMap.put("deptCounts",sysDepts.size());
        return resultMap;
    }

}
